<?php

namespace apiend\modules\work\models;

use Yii;
use common\models\work\SurveyRound;

/**
 * SurveySearch is the model class for table "{{%book}}".
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SurveySearch extends \common\models\work\Resident {

    /**
     * @var string 关键字
     */
    public $keyword;

    /**
     * @var string 关键字
     */
    public $checked;

    /**
     * @var string 关键字
     */
    public static $roundId = 1;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword', 'rid', 'checked'], 'safe'],
        ];
    }

    /**
     * Searching member apply
     * @param  array $params
     * @return \yii\data\ActiveDataProvider
     */
    public function buildQuery($params = []) {
        $query = static::find()->alias('t');
        if (!empty($params)) {
            $query->andFilterWhere($params);
        }
        $query->andFilterWhere([
            'or',
            ['t.mobile' => $this->keyword],
            ['t.idcardno' => $this->keyword],
            ['like', 't.address', $this->keyword],
        ]);
        if ($this->checked) {
            $query->andWhere([
                't.round_id' => self::$roundId
            ]);
        } else {
            $query->andWhere([
                '!=', 't.round_id', self::$roundId
            ]);
        }
        $query->orderBy([
            't.updated_at' => SORT_DESC
        ]);
        return $query;
    }

    public function getCheckedTime() {
        if ($this->round_id === self::$roundId) {
            return $this->updated_at;
        }
        return 0;
    }

    public function getCheckedStatus() {
        return self::$roundId === $this->round_id;
    }

    public function getRid() {
        return self::$roundId;
    }

    /**
     * {@inheritdoc}
     */
    public function fieldConfigs() {
        return [
            [
                'attribute' => 'id',
                'headerOptions' => ['width' => 80],
            ],
            [
                'attribute' => 'rid',
                'headerOptions' => ['width' => 80],
            ],
            [
                'attribute' => 'realname',
            ],
            [
                'attribute' => 'mobile',
            ],
            [
                'attribute' => 'idcardno',
            ],
            [
                'attribute' => 'address',
                'headerOptions' => ['width' => 200],
            ],
            [
                'attribute' => 'checkedStatus',
                'label' => '是否已检测',
                'format' => 'boolean',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'checkedTime',
                'label' => '检测时间',
                'format' => 'datetime',
                'options' => [
                    'nullText' => '-',
                    'nullValue' => 0,
                ],
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getRoundList() {
        return SurveyRound::find()->active()->all();
    }

    /**
     * {@inheritdoc}
     */
    public function configs() {
        return [
            'config' => [
                'filterConfig' => [
                    [
                        'label' => '关键字',
                        'prop' => 'keyword',
                        'type' => 'text',
                        'placeholder' => '姓名/手机号/身份证号/住址',
                    ],
                    [
                        'label' => '检测状态',
                        'placeholder' => '按核酸检测状态筛选',
                        'prop' => 'checked',
                        'type' => 'select',
                        'multiple' => false,
                        'valueKey' => 'value',
                        'labelKey' => 'label',
                        'data' => [
                            [
                                'value' => 1,
                                'label' => '已检测',
                            ],
                            [
                                'value' => 0,
                                'label' => '未检测',
                            ]
                        ],
                    ],
                ],
                'gridConfig' => $this->options(),
            ]
        ];
    }

}
